/*	AboutDialog.java
 *
 *	Copyright (c) Manateem Challenge Group, Zaia Group, and contributors 2010
 *
 *	This file is part of Manatee ('this project').
 *
 *	This project is free software: you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, either version 3 of the License, or (at your option)
 *	any later version.
 *
 *	This project is distributed in the hope that it will be useful, but WITHOUT
 *	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *	more details.
 *
 *	You should have received a copy of the GNU General Public License along with
 *	this project. If not, see <http://www.gnu.org/licenses/>.
 */
package edu.bu.manateem.msg.gui;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import edu.bu.manateem.msg.MSGConstants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *	<p>
 *	
 *	</p>
 *
 *
 *	@author
 *		Christopher Jacobs
 *
 *	@version
 *		1.0
 *
 *	@changelog
 *		Jun 14, 2010	- Version 1.0.
 */
public class AboutDialog
	extends JDialog
	implements HyperlinkListener
{
	/*
	 * TYPES
	 */

	/*
	 * CONSTANTS
	 */

	// Serialize
	private static final long serialVersionUID = 1L;
	// Logging
	private static final Logger LOG =
		LoggerFactory.getLogger(DebugDialog.class);
	
	// Credits
	private static final HashMap<String,String> CREDITS =
		new HashMap<String,String>()
		{
			// Serialize
			private static final long serialVersionUID = 1L;
			
			// Name => URL
			{
				put("JFreeChart","http://www.jfree.org/jfreechart/");
				put("Seattle Proteome Center (JRAP)",
					"http://tools.proteomecenter.org/wiki/index.php?title=Software:JRAP");
				put("Apache XML Graphics Project (batik)",
					"http://xmlgraphics.apache.org/batik/");
				put("SLF4J","http://www.slf4j.org/");
				put("Pacific Northwest National Laboratory", 
						"http://omics.pnl.gov/");
				put("The Computational Proteomics Laboratory (CPL) at Fred Hutchinson " +
						"Cancer Research Center (FHCRC)", 
						"http://proteomics.fhcrc.org/CPL/home.html");
			}
		};
	
	/*
	 * FIELDS
	 */

	JTextPane infoPane = new JTextPane();
	JButton okButton = new JButton("OK");
	
	/*
	 * CONSTRUCTORS
	 */

	public AboutDialog()
	{
		super();
		
		AboutDialog.LOG.debug("init self");
		
		initBody();
		init();
	}
	
	/*
	 * INITIALIZERS
	 */

	/*
	 * METHODS
	 */

	/**
	 *	<p>
	 *	
	 *	</p>
	 *
	 *	@since
	 *		
	 */
	@Override
	public void hyperlinkUpdate(HyperlinkEvent e)
	{
		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
		{
			try
			{
				if (Desktop.isDesktopSupported())
				{
					Desktop.getDesktop().browse(new URI(e.getDescription()));
				}
				else
				{
					JOptionPane.showMessageDialog(this,
						"Cannot open hyperlink -- unsupported feature.",
						"Error",JOptionPane.ERROR_MESSAGE);
				}
			}
			catch (IOException x)
			{
				JOptionPane.showMessageDialog(this,
					"Cannot open hyperlink -- IO exception","Error",
					JOptionPane.ERROR_MESSAGE);
			}
			catch (URISyntaxException x)
			{
				JOptionPane.showMessageDialog(this,
					"Cannot open hyperlink -- invalid URI","Error",
					JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	public void init()
	{
		AboutDialog.LOG.debug("build dialog");
		
		// behavior
		setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		setTitle("About Manatee");
		
		// layout
		// setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
		
		// finish
		add(infoPane);
		pack();
		
		AboutDialog.LOG.debug("build dialog -- DONE");
	}
	
	public void initBody()
	{
		// Basic information
		AboutDialog.LOG.debug("build info pane");
		infoPane.addHyperlinkListener(this);
		infoPane.setContentType("text/html");
		infoPane.setEditable(false);
		AboutDialog.LOG.debug("build info pane -- DONE");
		
		StringBuilder credits = new StringBuilder("<HTML>");
		credits.append("<H3>");
		credits.append(MSGConstants.APPLICATION_NAME);
		credits.append("</H3>");
		credits.append("Version: " + MSGConstants.APPLICATION_VERSION);
		credits.append("<BR><BR>");
		credits.append(MSGConstants.APPLICATION_COPYRIGHT);
		credits.append("<BR><BR>");
		
		// Credits
		credits.append("This project includes software provided by:");
		credits.append("<ul>");
		for (String creditee : AboutDialog.CREDITS.keySet())
		{
			credits.append("<li>");
			credits.append(creditee);
			credits.append(" { ");
			credits.append("<a href=\"");
			credits.append(AboutDialog.CREDITS.get(creditee));
			credits.append("\">website</a>");
			credits.append(" } ");
			credits.append("</li>");
		}
		credits.append("</ul>");
		credits.append("</HTML>");
		infoPane.setText(credits.toString());
		
		// Other
	}
}
